var DetailsFun={
	inits:function(){
		this.ajax();
		this.addcaigoudan();
		this.buyNowFun();
	},
	Interface:{
		goodsDetail:"http://ww.yc630.com/api/index.php/Buyer/Goods/goodsDetail", //详情页
		addcaigoudan:"http://ww.yc630.com/api/index.php/Buyer/Order/addcaigoudan",//采购单
		hotsellGoods:"http://ww.yc630.com/api/index.php/Buyer/Goods/hotsellGoods", //热卖商品
		sellerdetail:"http://ww.yc630.com/api/index.php/Buyer/Goods/sellerdetail",   //商家详情
		goodsEvaluation:"http://ww.yc630.com/api/index.php/Buyer/Goods/goodsEvaluation", //评论
		buyNow:"http://ww.yc630.com/api/index.php/Buyer/Order/buyNow",//立即购买
		isLogin:"http://ww.yc630.com/api/index.php/Buyer/Login/isLogin",  //是否登录
		getTemptoken:"http://ww.yc630.com/api/index.php/Buyer/Login/getTemptoken"  //为登录状态获取token
	},
	ajax:function(){
		//console.log(token)
		var goods_id=common.Geturl().goods_id;
		common.ajaxFun(DetailsFun.Interface.goodsDetail,"POST",{token:token,goods_id:goods_id},DetailsFun.DetailFun); //详情
		common.ajaxFun(DetailsFun.Interface.hotsellGoods,"POST",{token:token},DetailsFun.hotsellGoodsFun); //热卖商品
		common.ajaxFun(DetailsFun.Interface.sellerdetail,"POST",{token:token,goods_id:goods_id},DetailsFun.sellerdetailFun); //商家详情
		common.ajaxFun(DetailsFun.Interface.goodsEvaluation,"POST",{token:token,goods_id:goods_id},DetailsFun.goodsEvaluationFun); //评论

	},
	html_decode:function (str){
	    var s = "";
	    if (str.length == 0) return "";
	    s = str;
	    s = s.replace(/&amp;/g, "&");
	    s = s.replace(/&lt;/g, "<");
	    s = s.replace(/lt;/g, "<");
	    s = s.replace(/&gt;/g, ">");
	    s = s.replace(/gt;/g, ">");
	    s = s.replace(/&nbsp;/g, " ");
	    s = s.replace(/&#39;/g, "\'");
	    s = s.replace(/&quot;/g, "\"");
	    s = s.replace(/<br>/g, "\n");
	    return s;
	},
	goodsEvaluationFun:function(res){
		var str="";
		$.each(res.data,function(i,v){
			str+='<div class="Reviews">'
			if(v.avatar=="") {
				str+='<div class="heade"><img src="../images/Group 19.png" alt=""></div>'
			}else{
				str+='<div class="heade"><img src="'+v.avatar+'" alt=""></div>'
			}
				str+='<div class="CommentContent">'+
					'<div><span>'+v.nickname+'：</span><em>'+v.evaluation+'</em></div>'+
					'<p>'+v.createtime+'</p>'+
				'</div>'+
			'</div>';
		});
		$("#BuyerReviews").append(str);
	},
	sellerdetailFun:function(res){
		// $("#commodityLogo img").attr("src",res.data.licence_image);
		$("#commodityHead img").attr("src",res.data.title_image);
	},
	hotsellGoodsFun:function(res){
		var str="";
			$.each(res.data,function(i,v){
				if (typeof v==="object") {
				str+='<div class="commodityHot" goods_id='+v.goods_id+'>'+
					'<img src='+v.image+' alt="">'+
					'<p>'+v.goods_name+'</p>'+
				'</div>';
				};
			});
		
		$("#SellingGoods").append(str);
		$("#SellingGoods").on("click","div.commodityHot",function(){//点击热销商品进入详情页
			window.location.href="CommodityDetails.html?goods_id="+$(this).attr("goods_id");
		})
	},
	DetailFun:function(res){
		$("#goBrand").attr("href","./brandHome.html?people_style="+res.data.goodmsg.people_style+"&brand="+res.data.goodmsg.brand+"&seller_id="+res.data.goodmsg.seller_id)
		var str="",attrStr="",
		smell_img=res.data.goodmsg.image.split(",");
		$("#MagTargetImg").attr("src",smell_img[0]);
		$.each(smell_img,function(i,v){
			str+='<li><img src='+v+'></li>';
		});
		$("#spec-items ul").html(str);
		$("#spec-items ul li").eq(0).addClass("on");
		
		MagnifierF("MagnifierWrap2");//调取放大镜

		var reduce=$("#reduce"),
			PurchaseNum=$("#PurchaseNum"),
			add=$("#add"),
			PresentPrice=$("#PresentPrice"),
			TotalPrice=$("#TotalPrice");
			$("#freight_status").val(res.data.goodmsg.freight_status);
			$("#seller_id").val(res.data.goodmsg.seller_id);
			//console.log(res.data);
			if (res.data.nomalattr!=null) {
				$.each(res.data.nomalattr,function(i,v){
					attrStr+='<div class="InforList nomalattr" id="nomalattr">'
								+'<span>'+i+'：</span>'
							$.each(v,function(a,b){
								attrStr+='<p>'+b+'</p>'
							})
							attrStr+='</div><div class="clear"></div>';
				});
			};

			$.each(res.data.priceattr,function(i,v){
				if (i=="统一定价"){
					attrStr+='<div class="InforList priceattr" id="priceattr" style="display:none;">'
				}else{
					attrStr+='<div class="InforList priceattr" id="priceattr">'
				};
				attrStr+='<span>'+i+'：</span>'
				$.each(v,function(a,b){
					attrStr+='<p price='+b.price+' sell_price='+b.sell_price+'>'+a+'</p>'
				})
				attrStr+='</div><div class="clear"></div>';
				
			});
			$("#CommodityAttribute").html(attrStr);
			$(".nomalattr").each(function(i,v){
				$(this).find("p").eq(0).addClass("on");
			});
			$(".priceattr").each(function(i,v){
				$(this).find("p").eq(0).addClass("on");
			});
			$("#PresentPrice em").html("¥"+$("#priceattr p.on").attr("sell_price"));
			$("#OriginalPrice em").html("¥"+$("#priceattr p.on").attr("price"));


			$(".nomalattr").on("click","p",function(){
				$(this).addClass("on").siblings("p").removeClass("on");
			});

			$(".priceattr").on("click","p",function(){
				$(this).addClass("on").siblings("p").removeClass("on");
			});

				TotalPrice.find("b").html("¥"+(Number($(this).attr("sell_price"))*res.data.goodmsg.minnu).toFixed(2));

			$(".priceattr").on("click","p",function(){
				$(this).addClass("on").siblings("p").removeClass("on");
				$("#PresentPrice em").html("¥"+$(this).attr("sell_price"));
				$("#OriginalPrice em").html("¥"+$(this).attr("price"));
				TotalPrice.find("b").html("¥"+(Number($(this).attr("sell_price"))*res.data.goodmsg.minnu).toFixed(2));
			});


		$("#OriginalPrice em").html(res.data.priceattr.price)	
		$("#design").html(res.data.goodmsg.goods_name); 
		//$("#fengge").html(res.data.goodmsg.describe);
		$("#TotalSupply em").html(res.data.goodmsg.store_nu);
		$("#ShippingAddress em").html(res.data.goodmsg.shipaddress_name);
		if(res.data.goodmsg.freight_status==1){
			$("#freight em").html("卖家承担运费");
		}else if(res.data.goodmsg.freight_status==0){
			$("#freight em").html("买家承担运费");
		}
		var describe=DetailsFun.html_decode(res.data.goodmsg.describe);
		$("#de").html(describe)
		
		if (res.data.goodmsg.minnu===null) {
			PurchaseNum.val(0);
		}else if (res.data.goodmsg.minnu!=null) {
			PurchaseNum.val(res.data.goodmsg.minnu);
		};
		//console.log(Number(PresentPrice.find("em").text().substring(1)))
		TotalPrice.find("b").html("¥"+(Number(PresentPrice.find("em").text().substring(1))*PurchaseNum.val()).toFixed(2));
		TotalPrice.find("i").html(PurchaseNum.val());
		DetailsFun.addReduce(reduce,1,res);//减－
		DetailsFun.addReduce(add,0,res);//加－
	},
	addReduce:function(ele,addOrReduce,res){
		var reduce=$("#reduce"),
			PurchaseNum=$("#PurchaseNum"),
			add=$("#add"),
			PresentPrice=$("#PresentPrice"),
			TotalPrice=$("#TotalPrice");
		ele.on("click",function(){
			if(addOrReduce==1){
				if (PurchaseNum.val()<=res.data.goodmsg.minnu) {
					alert("采购量不能小于起定量");
					return;
				}else {
					TotalPrice.find("i").html(PurchaseNum.val()-1);
					PurchaseNum.val(PurchaseNum.val()-1);
				};
				TotalPrice.find("b").html("¥"+(Number(PresentPrice.find("em").text().substring(1))*PurchaseNum.val()));
			}else if (addOrReduce==0) {
				//console.log(Number(PurchaseNum.val())>Number(res.data.goodmsg.store_nu))
				if (Number(PurchaseNum.val())>=Number(res.data.goodmsg.store_nu)) {
					alert("采购量不能大于供应量");
					return;
				}else {
					TotalPrice.find("i").html(Number(PurchaseNum.val())+1);
					PurchaseNum.val(Number(PurchaseNum.val())+1);
				};
				TotalPrice.find("b").html("¥"+(Number(PresentPrice.find("em").text().substring(1))*PurchaseNum.val()).toFixed(2));			
			}
			
		});
	},
	addcaigoudan:function(){
		//alert(common.getCookie("tokenNo"))
		$("#jrgwc").on("click",function(){
			if ($("#PurchaseNum").val()==0) {
				alert("数量不能为0");
				return false;
			};
			var nomalattr="",
				priceattr=$("#priceattr span").text()+$("#priceattr p.on").text(),
				goods_attr="";
				

				$(".nomalattr").each(function(i,v){
					nomalattr+=$(this).children("span").text()+$(this).children("p.on").text()+"；";
				});
				goods_attr=nomalattr+priceattr;
				var caigoudanObj={
					goods_id:common.Geturl().goods_id,
					goods_name:$("#design").html(),
					freight_status:$("#freight_status").val(),
					goods_attr:goods_attr,
					goods_price:$("#PresentPrice em").html().substring(1),
					goods_nu:$("#PurchaseNum").val(),
					total_money:$("#TotalPrice").find("b").html().substring(1),
					seller_id:$("#seller_id").val()
				}

 				common.ajaxFun(DetailsFun.Interface.isLogin,"POST",{token:token},function(res){
 					if (res.code==0) {//登录
 						caigoudanObj.token=token;
 		common.ajaxFun(DetailsFun.Interface.addcaigoudan,"POST",caigoudanObj,DetailsFun.jrgwcFun); //加入购物车

 					}else if (res.code==1) {//未登录
 						if (common.getCookie("Temptoken")=="") { //第一次向购物车加商品
			 				common.ajaxFun(DetailsFun.Interface.getTemptoken,"POST",{},function(tokenres){
			 					if (tokenres.code==1) {
			 						caigoudanObj.token=tokenres.data.token;
			 						common.setCookie("Temptoken",tokenres.data.token)//存入临时的token
			 					}else {
			 						alert(tokenres.msg)
			 					};
 		common.ajaxFun(DetailsFun.Interface.addcaigoudan,"POST",caigoudanObj,DetailsFun.jrgwcFun); //加入购物车
			 				});
		 				}else if (common.getCookie("Temptoken")!="") {
		 					caigoudanObj.token=common.getCookie("Temptoken");
		common.ajaxFun(DetailsFun.Interface.addcaigoudan,"POST",caigoudanObj,DetailsFun.jrgwcFun); //加入购物车
		 				};	
 		
 					};
 				})
		});	
	},
	buyNowFun:function(){
		$("#ljdg").on("click",function(){
			if ($("#PurchaseNum").val()==0) {
				alert("数量不能为0");
				return false;
			};
			var nomalattr="",
				priceattr=$("#priceattr span").text()+$("#priceattr p.on").text(),
				goods_attr="";
				

				$(".nomalattr").each(function(i,v){
					nomalattr+=$(this).children("span").text()+$(this).children("p.on").text();
				});
				goods_attr=nomalattr+priceattr;
			var caigoudanObj={
				goods_id:common.Geturl().goods_id,
				goods_name:$("#design").html(),
				freight_status:$("#freight_status").val(),
				goods_attr:goods_attr,
				goods_price:$("#PresentPrice em").html().substring(1),
				goods_nu:$("#PurchaseNum").val(),
				total_money:$("#TotalPrice").find("b").html().substring(1),
				seller_id:$("#seller_id").val()
			}
			common.ajaxFun(DetailsFun.Interface.isLogin,"POST",{token:token},function(res){
				if (res.code==0) {//登录
					caigoudanObj.token=token;
				}else if (res.code==1) {//未登录
    			alert("请登录！");
    			console.log(window.location.href)
				common.setCookie("hrefs",window.location.href);
    			window.location.href="./login/login.html";		
			};


				common.ajaxFun(DetailsFun.Interface.buyNow,"POST",caigoudanObj,function(res){//立即购买
					if (res.code==0) {
						window.location.href="./confirmOrder.html?orderid="+res.data.orderid;
					}else{
						alert(res.msg)
					};
				}); 
			});
		});		
	},
	jrgwcFun:function(res){
		if(res.code==0){
			$(".mask,#tjgwcOk").show();
		}else {
			alert(res.msg);
		}
		$("#close span").on("click",function(){
			$(".mask,#tjgwcOk").hide();
		});
		$("#gouOrder").on("click",function(){
			window.location.href="./PurchaseOrder.html";

		});
		$("#ContinueShoping").on("click",function(){
			$(".mask,#tjgwcOk").hide();
		});
	}
}
DetailsFun.inits();


